define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yungift/card/main/index',
                    add_url: 'yungift/card/main/add',
                    edit_url: 'yungift/card/main/edit',
                    // del_url: 'yungift/card/main/del',
                    multi_url: 'yungift/card/main/multi',
                }
            });

            var table = $("#table");

            // 初始化表格template
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        // 该列为复选框字段,如果后台的返回state值将会默认选中
                        {
                            field: 'state',
                            checkbox: true
                        },
                        // sortable为是否排序,operate为搜索时的操作符,visible表示是否可见
                        {
                            field: 'id',
                            title: 'ID',
                            sortable: true,
                            operate: false
                        },
                        // 模糊搜索
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'start_time',
                            title: __('开始时间'),
                            sortable: true,
                            operate: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'end_time',
                            title: __('结束时间'),
                            sortable: true,
                            operate: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'chance_count',
                            title: __('可兑机会'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'note',
                            title: __('备注'),
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: Config.statusList,
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'updated_at',
                            title: __('Update time'),
                            sortable: true,
                            operate: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        // 操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: '150px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'plus',
                                    text: __('二维码'),
                                    title: __('入口二维码'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-qrcode',
                                    url: function (row) {
                                        return 'yungift/qrcode/preview?scene=card&cid=' + row.id;
                                    },
                                    extend: 'data-area=\'["300px","500px"]\'',
                                },
                                {
                                    name: 'code',
                                    text: __('卡密'),
                                    title: function (row) {
                                        return __('卡密管理') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-tags',
                                    url: function (row) {
                                        return 'yungift/card/code/index?card_id=' + row.id;
                                    },
                                },
                                {
                                    name: 'gift',
                                    text: __('礼品'),
                                    title: function (row) {
                                        return __('关联礼品') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-gift',
                                    url: function (row) {
                                        return 'yungift/gift/main/index?model_type=card&model_id=' + row.id;
                                    },
                                },
                                {
                                    name: 'order',
                                    text: __('订单'),
                                    title: function (row) {
                                        return __('相关订单') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'yungift/gift/order/index?model_type=card&model_id=' + row.id;
                                    },
                                },
                                {
                                    name: 'preview',
                                    text: __('预览'),
                                    title: function (row) {
                                        return __('预览礼卡') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-share-square-o',
                                    url: function (row) {
                                        return 'yungift/qrcode/precard?cid=' + row.id;
                                    },
                                    extend: 'data-area=\'["300px","500px"]\'',
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ],
                ],
                // 快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                // 启用普通表单搜索
                commonSearch: true,
                // 显示切换视图（table/card）按钮
                showToggle: false,
                // 显示导出按钮
                showExport: false,
                // 启用跨页选择
                maintainSelected: true,
                // 启用固定列
                fixedColumns: true,
                // 固定右侧列数
                fixedRightNumber: 1,
                // 可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            table.on('load-success.bs.table', function (data) {
                // 编辑窗口大小
                $(".btn-editone").data('area', ['100%', '100%']);
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: { // 绑定事件的方法
            }
        }
    };
    return Controller;
});
